﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Appology.SugarSync.Explorer.Phone.Converters
{
    public class FileLengthBytesToTextConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is long)
            {
                long length = (long) value;

                if (length >= 1073741824)
                {
                    // GB
                    return string.Format("{0:0.00} GB", length / 1073741824.0);
                }

                if(length >= 1048576)
                {
                    // MB
                    return string.Format("{0:0.00} MB", length / 1048576.0);
                }

                if(length > 1024)
                {
                    // K
                    return string.Format("{0:0.00} K", length / 1024.0);
                }

                return string.Format("{0} Bytes", length);
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}